package com.glodon.paas.account.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.glodon.paas.account.biz.UserAdviceBiz;
import com.glodon.paas.account.biz.UserAdviceReplyBiz;
import com.glodon.paas.account.biz.UserBiz;
import com.glodon.paas.account.dao.domain.UserAdviceEntity;
import com.glodon.paas.account.dao.domain.UserAdviceReplyEntity;
import com.glodon.paas.account.dao.domain.UserEntity;
import com.glodon.paas.account.service.UserAdviceService;
import com.glodon.paas.mail.SpringMailMessage;
import com.glodon.paas.mail.SpringMailSender;

@Service("userAdviceService")
public class UserAdviceServiceImpl implements UserAdviceService {
	@Autowired
	private UserAdviceBiz userAdviceBiz;
	@Autowired
	private SpringMailSender mailSender;

	@Autowired
	private UserBiz userBiz;
	@Autowired
	private UserAdviceReplyBiz  userAdviceReplyBiz;
	
	@Override
	public UserAdviceEntity createAdvice(UserAdviceEntity record) {
		record = userAdviceBiz.insert(record);

		UserEntity user = userBiz.getUser(record.getUserId());
		
		//发送邮件
		Map<String, Object> templateParam = new HashMap<String, Object>();
		templateParam.put("adviceTitle", record.getTitle());
		templateParam.put("adviceContent", record.getContent());
		
		//用户信息
		templateParam.put("userId", record.getUserId());
		templateParam.put("userEmail", user.getEmail());
		templateParam.put("userMobile", user.getMobile()==null?"":user.getMobile());		
		if(StringUtils.isNotBlank(user.getNickname())){
			templateParam.put("userName", user.getRealname());
		}else if(StringUtils.isNotBlank(user.getRealname())){
			templateParam.put("userName", user.getNickname());
		}else{
			templateParam.put("userName", user.displayName());
		}
		
		SpringMailMessage mailContent = new SpringMailMessage();
		mailContent.setTo("cloud@glodon.com");
		mailContent.setSubject("用户留言："+record.getTitle());
		//mailContent.setText(record.getContent());
		mailContent.setTemplateLocation("mailTemplate/account_userAdvice.vm");
		mailContent.setTemplateParam(templateParam);
		mailSender.send(mailContent );
		return record; 
	}

	@Override
	public UserAdviceEntity getAdviceById(String id) {
		return userAdviceBiz.selectByPrimaryKey(id);
	}

	@Override
	public List<UserAdviceEntity> queryAdvice(String userId, Integer status,
			Integer type, Integer deleteStatus, Integer startIndex,Integer count) {
		return userAdviceBiz.queryAdvice(userId, status, type, deleteStatus, startIndex, count);
	}

	/* (non-Javadoc)
	 * @see com.glodon.paas.account.service.UserAdviceService#createReply(com.glodon.paas.account.dao.domain.UserAdviceReplyEntity)
	 */
	@Override
	public void createReply(UserAdviceReplyEntity entity) {
		userAdviceReplyBiz.insert(entity);
		
	} 
	@Override
	public List<UserAdviceReplyEntity> queryReply(String adviceId,
			Integer startIndex, Integer count) {
		return userAdviceReplyBiz.selectByAdviceId(adviceId, startIndex, count);
	}
  
}
